#include "Include.h"

vu8 PanMoveInDet;
vu8 StartWorkCount;
vu8 PanDetPulseCnt;
vu8 NoPanDetectedCnt;

vu16 PPGTA_buf;
void Prepare_PanDetect()
{
    PanMoveInDet = C_PAN_DET_TIMER_100MS;
    IGBT_Off();
    PPGTA_Min = C_MinPPGT;
}

vu8 OffCurrent_TH = 0;
void PanMoveOut_Detect() // run every 20mS
{ 	 
 	static vu8 OffCurrent_Cnt = 0;
	//vu8 OffCurrent_TH = 0;
 
    if (bMachineOnState == ON) //
    {
        if ((bPowerState == 1) && (StartWorkCount > 25))
        {      			
			if (1 == bLowPower)
			{	
				if(Statu_LowPow_Work ==2   && LowPow_Cnt < LowPow_ON)	
				{
					PPGTA_buf = _ppgta8 << 8;
					PPGTA_buf = PPGTA_buf + _ppgta;	
				}
							
				if(PPGTA_buf <= C_MinPPGT_l)//20us 192
				{
					OffCurrent_TH = 25;	//40
				}
				else if(PPGTA_buf <= PANOUT_PPGTA_2)//16~19us 272
				{
					OffCurrent_TH = 20;	//30
				}	
				else if(PPGTA_buf <= PANOUT_PPGTA_3)//13~15us  304
				{
					OffCurrent_TH = 14;	//20
				}
				else// <=11us
				{
					OffCurrent_TH = 12;//13;	
				}
			}
			else
			{
				PPGTA_buf = _ppgta8 << 8;
				PPGTA_buf = PPGTA_buf + _ppgta;	

				if(PPGTA_buf<= C_MinPPGT)//30us
				{
					OffCurrent_TH = 50;	
				}
				else if(PPGTA_buf <= PANOUT_PPGTA_1)//20~29us
				{
					OffCurrent_TH = 45;	
				}
				else if(PPGTA_buf <= PANOUT_PPGTA_2)//15~19us
				{
					OffCurrent_TH = 40;
				}
				else// <=14
				{
					OffCurrent_TH = 30;
				}
			}
			
			//------------------------------------------
            if (AD_Iac < OffCurrent_TH) 
            {
                OffCurrent_Cnt++;
                if (OffCurrent_Cnt > 8) // 8*20ms=160ms
                {
                    OffCurrent_Cnt = 0;
                    Prepare_PanDetect();//power off Prepare PanDetect
                }
            }
            else
            {
                OffCurrent_Cnt = 0;
            }
        }
    }
}

u8 Pan_Detect_Cnt()//Return the number of pan detection pulses
{
    u8 v_IntCnt = 0;
    volatile bit v_buf2 = 0;
    u8 i;
    if (_c2cmpop == 1) // No surge detected, pot inspection, no pot inspection when there is a surge
    {
        _emi = DISABLE;

		_ppgsamd=0;		//SW Mode
		_ppgsaen=0;

        _ppgta8 = 1;

		if(bInitSet_pulseWidth == 1)
		{
			_ppgta = (256-pulseWidth_buf*16);	//I2C init setting pilse width
		}
		else
		{
		    _ppgta = C_PAN_DETECT_PPGTA; //default 7us
		}

    	//InitSetting_PanDetPulseWidth(); //
	
       _ppgc0 = 0b10000110;	//Start PPG, enable comparator 2 and 3 to stop PPG counting
       
       
	   _rlbf = 0;
	   _ptsyn = 0;
	   _pcsd = 0;
	   _trgmod = 1;
	   _c1rlen = 0;
        while (_pst0 == 1); // Wait PST bit low

        for (i = 0; i < 230; i++) // Detection times	1ms
        {
            if (_c0cmpop != v_buf2)
            {
            	GCC_NOP();
                if (_c0cmpop != v_buf2)
	            {
	            	v_buf2 = _c0cmpop;	
	          		v_IntCnt++; //
	            } 
            }       
        }
        _emi = ENABLE;
    }
    return v_IntCnt/2;// Pulse count
}

vu8 PanDetPulseNum = 0;
void PanMoveIn_Detect() // run every 100mS
{
//	vu8 PanDetPulseNum = 0;
    // Prepare for pot inspection
    if ((PanMoveInDet == 0) && (bPowerState == 0) && (bErrPowerOff == 0))
    {
        Prepare_PanDetect();
    }

    if (bMachineOnState == ON)
    {
        if (PanMoveInDet > 1)
        {
        	PanMoveInDet--;
        }
        else if ((PanMoveInDet == 1) && (bPowerState == 0) && (bErrPowerOff == 0)) // init
        {
            PanMoveInDet = 0;
            
			//Set the number of pan detection pulses
            if(bInitSet_pulseNum == 1)
            {
            	PanDetPulseNum = pulseNum_buf;	//init setting mode  value
            }
            else
            {
            	PanDetPulseNum = PAN_DETECT_PULSE_NUM;	//default value
            }
            
            PanDetPulseCnt = Pan_Detect_Cnt(); // Check pot will close interrupt

            if (PanDetPulseCnt == 0)
            {
                PanDetPulseCnt = 255;
            }
            
            if (PanDetPulseCnt > PanDetPulseNum)
            {
                // no pan
                IH_Status_App.flagBit.NoPan = NO_PAN;
                Beep_Cmd = BEEP_200MS;

                if (NoPanDetectedCnt < C_NoPanDetectedOff)
            	{
            		NoPanDetectedCnt++;	
            	}   
                else
                {
                    bErrPowerOff = 1;
                    NoPanDetectedCnt = 0;
                    ErrRecoveryTimer = C_ErrRecoveryTime;
                    if (bMachineOnState == ON)
                    {
                        PowerOffProc();
                        Beep_Cmd = BEEP_200MS;
                    }
                }
            }
            else
            {
                // have pan
                IH_Status_App.flagBit.NoPan = HAVE_PAN;
                NoPanDetectedCnt = 0;
                if (bPowerState == 0)
                {
                    if (bLowPower == 0)
                    {
                        IGBT_PowerOn();
                        StartWorkCount = 0;
                    }
                    else if (bLowPower == 1)
                    {
                        bPowerState = 1;
                        StartWorkCount = 0;

                        if (0 == Statu_LowPow_Work)
                        {
                            Statu_LowPow_Work = 1;
                        }
                    }
                }
            }
        }
    }
}

